/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using ResourceManager = FourRoads.Links.Controls.ResourceManager;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for SectionTrackingButton.
	/// </summary>
	public class SectionTrackingButton : ResLinkBaseTemplatedWebControl
	{
		#region Child Controls
		SectionTrackedLinkButton	_Notification = null;
		#endregion

		protected override void AttachChildControls()
		{
			_Notification = FindControl("SectionNotification") as SectionTrackedLinkButton;
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);

			if (!Page.IsPostBack)
			{
				DataBind();
			}
		}

		public override void DataBind()
		{
			base.DataBind();

			Page.RegisterStartupScript(this.ClientID + "NotifyHide" , string.Format(clientScriptDisplayNone , _Notification.ClientID));

			if (ThisSection.SectionID > 0)
			{
				_Notification.SectionID = ThisSection.SectionID;
				_Notification.IsTracked = ThisSection.GetSectionSubscriptionType() == EmailSubscriptionType.None ? false : true;

				Page.RegisterClientScriptBlock(this.ClientID + "NotifyDisplay" , string.Format(clientScriptDisplay , _Notification.ClientID ));
			}
		}

		string clientScriptDisplayNone = @"<script language='javascript' type='text/javascript'>
			var objNotify = document.getElementById('{0}');
			if (objNotify != null)
				objNotify.style.display = 'none';
			</script>";

        //TODO: addEvent not working properly needs fixing
		string clientScriptDisplay = @"<script language='javascript' type='text/javascript'>
            var objNotify = document.getElementById('{0}');
            function addEvent(obj, evType, fn){{ 
             if (obj.addEventListener){{ 
               obj.addEventListener(evType, fn, false); 
               return true; 
             }} else if (obj.attachEvent){{ 
               var r = obj.attachEvent('on'+evType, fn); 
               return r; 
             }} else {{
               return false; 
             }} 
            }}

			addEvent(window, 'load', function()
                    {{if (objNotify != null)
					objNotify.style.display = 'inline';}}
            );
			</script>
		";
	}
}
